package com.iwdnb.gkgz.application.event;

import java.util.List;
import java.util.stream.Collectors;

import com.alibaba.fastjson.JSON;

import com.google.common.collect.Lists;
import com.iwdnb.bmnf.common.eventbus.EventHandler;
import com.iwdnb.bmnf.common.model.OrderBy;
import com.iwdnb.gkgz.application.analysis.StockAnalysisContext;
import com.iwdnb.gkgz.application.analysis.StockAnalysisContext.AnalysisParam;
import com.iwdnb.gkgz.application.analysis.StockAnalysisService;
import com.iwdnb.gkgz.application.model.request.AddStrategyTradeStockDataRequest;
import com.iwdnb.gkgz.application.service.StockService;
import com.iwdnb.gkgz.application.strategy.StockBackValidateService;
import com.iwdnb.gkgz.common.model.dto.Stock;
import com.iwdnb.gkgz.common.model.dto.StockDayData;
import com.iwdnb.gkgz.common.model.dto.StrategyTradeDTO;
import com.iwdnb.gkgz.common.utils.StockBlackListUtils;
import com.iwdnb.gkgz.infrastructure.dao.repo.StockDayDataRepository;
import com.iwdnb.wwzy.dao.MetaDataDAO;
import com.iwdnb.wwzy.model.query.MetaQuery;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static com.iwdnb.gkgz.common.constant.GkgzConstants.OBJECT_CODE_STRATEGY_TRADE;

@Component
@Slf4j
public class StockAnalysisEventHandler implements EventHandler<AddStrategyTradeStockDataRequest> {
    public static final String EVENT_TOPIC = "stockAnalysis";
    @Autowired
    protected StockService stockService;
    @Autowired
    protected StockDayDataRepository stockDayDataRepository;
    @Autowired
    private MetaDataDAO metaDataDAO;
    @Autowired
    private StockAnalysisContext stockAnalysisContext;

    @Override
    public String topic() {
        return EVENT_TOPIC;
    }

    @Override
    public void onMessage(AddStrategyTradeStockDataRequest request) {
        log.debug("doStockAnalysis,request:" + JSON.toJSONString(request));
        MetaQuery metaQuery = new MetaQuery();
        metaQuery.addParam("uuid", request.getStrategy());
        metaQuery.addParam("status", "sell");
        metaQuery.addParam("winFlag", "n");
        metaQuery.setOrder(new OrderBy("profit_rate", "desc"));
        List<StrategyTradeDTO> strategyTradeDTOList = metaDataDAO.queryListByCondition(OBJECT_CODE_STRATEGY_TRADE,
            metaQuery, StrategyTradeDTO.class);
        if (CollectionUtils.isEmpty(strategyTradeDTOList)) {
            log.warn("没有策略交易记录。");
            return;
        }
        AnalysisParam param = new AnalysisParam();
        param.setTotalCount(strategyTradeDTOList.size());
        for (StrategyTradeDTO strategyTradeDTO : strategyTradeDTOList) {
            String code = strategyTradeDTO.getCode();
            List<StockDayData> stockDayDataList = stockDayDataRepository.queryAll(code);
            if (CollectionUtils.isEmpty(stockDayDataList)) {
                continue;
            }
            stockAnalysisContext.analysisStock(code, stockDayDataList, strategyTradeDTO, param);
        }
        log.debug("最终结果:{}", JSON.toJSONString(param));
    }
}
